package com.ywyt.elasticsearch.service.impl;

import com.ywyt.elasticsearch.service.BaseSearchService;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Service
public class BaseSearchServiceImpl implements BaseSearchService {

    @Autowired
    private RestHighLevelClient client;
    @Override
    public List<Map<String, Object>> termSearch(String index, String type, String field, String value) throws IOException {
        //创建查询的request
        SearchRequest searchRequest = new SearchRequest(index);
        searchRequest.types(type);

        //封装条件
        /*
        * 先创建SearchSourceBuilder对象封装条件
        * 然后再放入searchRequest中
        * */
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.termQuery(field,value));
        searchRequest.source(searchSourceBuilder);

        //执行查询
        SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);

        //封装结果
        List<Map<String,Object>> result = new ArrayList<>();
        for (SearchHit hit : search.getHits().getHits()) {
            result.add(hit.getSourceAsMap());
        }
        return result;
    }

    @Override
    public List<Map<String, Object>> termsSearch(String index, String type, String field, String... values) throws IOException {
        //创建查询request
        SearchRequest searchRequest = new SearchRequest(index);
        searchRequest.types(type);
        //创建SearchSourceBuilder封装查询条件
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.termsQuery(field,values));
        searchRequest.source(searchSourceBuilder);
        //执行查询
        SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
        //封装结果
        List<Map<String,Object>> result = new ArrayList<>();
        for (SearchHit hit : search.getHits().getHits()) {
            result.add(hit.getSourceAsMap());
        }
        return result;
    }

    @Override
    public List<Map<String, Object>> matchAll(String index, String type) throws IOException {
        //创建SearchRequest
        SearchRequest searchRequest = new SearchRequest(index);
        searchRequest.types(type);
        //封装查询条件
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());
        searchRequest.source(searchSourceBuilder);
        //执行查询
        SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
        //封装结果
        List<Map<String,Object>> result = new ArrayList<>();
        for (SearchHit hit : search.getHits().getHits()) {
            result.add(hit.getSourceAsMap());
        }
        return result;
    }

    @Override
    public List<Map<String, Object>> match(String index, String type, String field, String value) throws IOException {
        //创建SearchRequest
        SearchRequest searchRequest = new SearchRequest(index);
        searchRequest.types(type);
        //封装查询条件
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.matchQuery(field,value));
        searchRequest.source(searchSourceBuilder);
        //执行查询
        SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
        //封装结果
        List<Map<String,Object>> result = new ArrayList<>();
        for (SearchHit hit : search.getHits().getHits()) {
            result.add(hit.getSourceAsMap());
        }
        return result;
    }

    @Override
    public List<Map<String, Object>> multiMatch(String index, String type, String[] field, String value) throws IOException {
        //创建SearchRequest
        SearchRequest searchRequest = new SearchRequest(index);
        searchRequest.types(type);
        //封装查询条件
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.multiMatchQuery(value,field));
        searchRequest.source(searchSourceBuilder);
        //执行查询
        SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
        //封装结果
        List<Map<String,Object>> result = new ArrayList<>();
        for (SearchHit hit : search.getHits().getHits()) {
            result.add(hit.getSourceAsMap());
        }
        return result;
    }
}
